package com.shijie.controller.app;

import com.github.pagehelper.PageHelper;
import com.shijie.component.AppUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.annotation.NoLogin;
import com.shijie.domain.entity.*;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.service.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/*
 *  APP 广告位，推荐位等接口
 *
 *
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app")
public class AppIndexPageController {

    @Resource
    private AdvertsService advertsService;

    @Resource
    private RecommendsService recommendsService;

    @Resource
    private ActivitysService activitysService;

    @Resource
    private MagazinesService magazinesService;

    @Resource
    private TalentsService talentsService;

    @Resource
    private AppUserService appUserService;

    @Resource
    private UserAttentionsService userAttentionsService;

    @Resource
    private CategoriesService categoriesService;

    /**
     * 视界首页
     *
     * @return
     */
    @GetMapping("worldIndex")
    @NoLogin
    public Result worldIndex() {
        List<Adverts> adverts = advertsService.selectAdverts("1", "0");
/*        List<Recommends> hotList = recommendsService.selectRecommends(3);
        List<Recommends> testList = recommendsService.selectRecommends(2);*/
        Condition condition = new Condition(Categories.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("status", 0);
        criteria.andEqualTo("parentId", 1);
        criteria.andEqualTo("type", 0);
        condition.orderBy("sort").asc();
        List<Categories> categories = categoriesService.selectByCondition(condition);
        return ResultGenerator.success().putData("adverts", adverts).putData("categories", categories);
    }


    /**
     * 品牌首页
     *
     * @return
     */
    @GetMapping("brandIndex")
    @NoLogin
    public Result brandIndex() {
        // adverts banner图
        List<Adverts> adverts = advertsService.selectAdverts("0", "0");
        // 首页的推荐的藏品配置数据
        List<Recommends> recommends = recommendsService.selectRecommends(0);
        // 首页的活动数据

        PageHelper.startPage(1, 5);
        Condition condition = new Condition(Activitys.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andLessThanOrEqualTo("startDate", new Date());
        criteria.andGreaterThanOrEqualTo("endDate", new Date());
        condition.orderBy("id").desc();
        List<Activitys> activitys = activitysService.selectByCondition(condition);

        // 猜你喜欢 推荐客户点击率最高分类的6个藏品
        List<Products> guess = userAttentionsService.userLikeProducts(appUserService.isLogin());

        return ResultGenerator.success().putData("adverts", adverts).putData("activitys", activitys).putData("recommends", recommends).putData("guess", guess);
    }

    /**
     * 达人首页
     *
     * @return
     */
    @GetMapping("talentIndex")
	public Result talentIndex() {
		// 最新的5个达人文章
		List<Magazines> list = magazinesService.searchForTalent(null, EnumInterface.Status.VALID.ordinal());
		userAttentionsService.attentions(appUserService.getCurrentUser().getId(), list);
		// 推荐5个未关注的达人 和最近的三个文章
		List<Talents> recommends = talentsService
				.recommendTalents(appUserService.getCurrentUser().getId(), 0, 5);
        //
        List<Adverts> adverts = advertsService.selectAdverts("2", "0");

		/*// 达人
		Users user = new Users();
		user.setUserType(EnumInterface.UserType.TALENT.ordinal());
		List<Users> talents = talentsService.selectTalentsByAll(user);*/
		return ResultGenerator.success().putData("magazines", list.subList(0, 5 > list.size() ? list.size() : 5))
				.putData("recommends", recommends).putData("adverts", adverts);

	}
}

